#! /bin/bash

# Gnulib files in zmanda repo are from gnulib GIT.  To check out the
# same version as was used to generate the source in the repository,
# run the following in your gnulib git repository:
#
#   git checkout cd9ecca8812571fc443688d11148f2768ededfa4

# get to the project root
cd `dirname $0`/../..
if [ ! -f configure.ac ]
then
    echo "Cannot navigate to project root"
    exit 1
fi

# check that gnulib-tool is available before we do anything drastic
[ -z "${GNULIB_TOOL}" ] && GNULIB_TOOL=gnulib-tool
"${GNULIB_TOOL}" --version >/dev/null 2>/dev/null
if [ "$?" != 0 ]
then
    echo "gnulib-tool not found; try using"
    echo "  GNULIB_TOOL=/path/to/gnulib-tool ${0} ${@}"
    exit 1
fi

# remove the files in gnulib and config/gnulib, but not directories
# (and, in particular, not the gnulib/regenerate directory)
shopt -s nullglob
for filename in gnulib/* config/gnulib/*
do
    [ -d "${filename}" ] && continue
    echo "Removing ${filename}"
    rm "${filename}" || exit 1
done
shopt -u nullglob

########################## DEVELOPERS! ###############################
## Add your modules here, then see below if you need to patch them. ##
######################################################################

GNULIB_MODULES="getaddrinfo lock base64 physmem fsusage"
GNULIB_MODULES="$GNULIB_MODULES mkdtemp getopt-gnu full-read full-write"
GNULIB_MODULES="$GNULIB_MODULES lseek fseeko ftello regex environ"
GNULIB_MODULES="$GNULIB_MODULES inet_pton nanosleep euidaccess"

######################################################################
## End modules                                                      ##
######################################################################

# create a brand new gnulib directory

"${GNULIB_TOOL}" --import \
        --dir=. --lib=libgnu --source-base=gnulib --m4-base=config/gnulib \
        --doc-base=doc --aux-dir=config --libtool --macro-prefix=gl \
        ${GNULIB_MODULES} || exit 1

########################## DEVELOPERS! ###############################
## Add your patch commands here, or use sed invoacations, etc.      ##
## Please comment heavily!                                          ##
######################################################################
PATCH_DIR="`pwd`/gnulib/regenerate"


# Patch Makefile.am to not error out on warnings
patch -p0 < "${PATCH_DIR}/no-error.patch" || exit 1
rm -f gnulib/Makefile.am.orig

# Patch fsusage.h to not use 'bool', as it doesn't play nicely with
# Perl on Mac OS X
patch -p1 < "${PATCH_DIR}/fsusage-no-bool.patch" || exit 1

# Add info to the makefile to ensure all this stuff gets put in
# the distribution tarball
(   echo ""
    echo "# Make sure regenerate/* are in the distribution tarball"
    echo "EXTRA_DIST += "`cd gnulib; find regenerate -name .svn -prune -o -type f -print | sort`
) >> gnulib/Makefile.am

# remove .gitignore files, which ignore things we would like to see
rm -f config/gnulib/.gitignore
rm -f gnulib/.gitignore

######################################################################
## End patches                                                      ##
######################################################################
